<?php
/**
 * Object Relational Persistence Mapping Class for table: district
 * @package	com.clearhealth
 * @author	ClearHealth Inc.
 */

class District extends ORDataObject {

	/**#@+
	 * Fields of table: district mapped to class members
	 */
	var $district_id		= '';
	var $province_id 		= '';
	var $vi_name	=	'';
	var $en_name	=	'';
	/**#@-*/
	
	/**
	 * DB Table
	 */
	var $_table = 'district';

	/**
	 * Primary Key
	 */
	var $_key = 'district_id';

	/**
	 * Internal Name
	 */
	var $_internalName = 'District';

	/**
	 * Handle instantiation
	 */
	public function District() {
		parent::ORDataObject();
	}

	public function getDistrictList($provinceId = 0){
		$field	=	L10nHelper::getLocale();
		$field	= 	L10nHelper::changeField($field,"name");
		$ds =& new Datasource_sql();
		
		$sql = array(
			'cols' 	=> "district_id, $field as name",
			'from' 	=> "$this->_table",
			'where'	=>	 "province_id =". (int)$provinceId,
			'orderby' => 'name'
			);
		$ds->setup($this->_db, $sql);
		return $ds->toArray('district_id', 'name');
	}
	/**
	 * @author hdviet
	 * update for localize demography
	 */
	public function getAllDistricts($provinceId){
		$field	=	L10nHelper::getLocale();
		$field	= 	L10nHelper::changeField($field,"name");
		$ds =& new Datasource_sql();
		$sql = array(
			'cols' 	=> "district_id as district_id, $field as name",
			'from' 	=> "$this->_table",
			'where'	=>	 "province_id =". (int)$provinceId,
			'orderby' => "name"
			);
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}
	
	//get Province by district_id
	/**
	 * @author hdviet
	 * update for localize demography
	 */
	public function getDistrict($distirctId){
		$field	=	L10nHelper::getLocale();
		$field	= 	L10nHelper::changeField($field,"name");
		$sql = array(
			'cols' => "district_id as district_id, $field as name",
			'from' => "$this->_table",
			'where' => "district_id = '$distirctId'");
		$ds = & new Datasource_sql();
		$ds->setup($this->_db, $sql);
		$result = $ds->toArray();
		if (empty($result)) return false;
			else return $result[0];
	}
	/**
	 * @author hdviet
	 * update for localize demography
	 */
	public function addDistrict($name, $provinceId){
	    $locales = L10nHelper::getSupportedLocales();
		$data = array();
       	foreach ( $locales as $lo => $la ) {
       		$lo	= 	L10nHelper::changeField($lo,"name");
       		$data[$lo] = $name;
       	}
		$data['province_id'] = $provinceId;
		$this->populateArray($data);
		$this->persist();
		return ;
	}
	/**
	 * @author hdviet
	 * update for localize demography
	 */
	public function updateDistrict($name, $districtId){
		$field		= 	L10nHelper::changeField(L10nHelper::getLocale(), "name");
		$data = array($field => $name);
        $this->setup($districtId);
        $this->populateArray($data);
        $this->persist();
	}
	/**
	 * @author hdviet
	 * update for localize demography
	 */
	public function checkDuplicateName($name, $provinceId){
		$field	=	L10nHelper::getLocale();
		$field		= 	L10nHelper::changeField($field,"name");
		
		$sql = array(
			'cols' 	=> "district_id, $field as name",
			'from' 	=> "$this->_table",
			'where' => "$field='$name' AND province_id = '$provinceId' ");
		$ds = & new Datasource_sql();
		$ds->setup($this->_db, $sql);
		$result = $ds->toArray();
		if (empty($result)) return false;
		foreach ( $result as $value ) {
			if (strcmp($name,$value['name']) == 0) {
				
				return true;
			}
		}
		return false;
	}


}
?>
